package com.shixi.shixitraining.shixitraining.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.ui.tabapply.fragment.ApplyFragment;
import com.shixi.shixitraining.shixitraining.ui.tabhome.fragment.HomeFragment;
import com.shixi.shixitraining.shixitraining.ui.tabmine.fragment.MineFragment;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;


public class MainActivity extends BaseActivity {

    private static boolean isExit = false;
    private FragmentTabHost mTabHost;
 private Handler handler=new Handler(){
     @Override
     public void handleMessage(Message msg) {
         isExit=false;
     }
 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTab();
    }

    private void initTab() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        //首页
        View homeIndicator = getIndicatorView(R.string.tabhost_home, R.drawable.selector_home);
        mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.tabhost_home))
                .setIndicator(homeIndicator), HomeFragment.class, null);

        //申请
        View lessonsIndicator = getIndicatorView(R.string.tab_apply, R.drawable.selector_apply);
        mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.tab_apply))
                .setIndicator(lessonsIndicator), ApplyFragment.class, null);


        //我的
        View mineIndicator = getIndicatorView(R.string.tabhost_mine, R.drawable.selector_mine);
        mTabHost.addTab(mTabHost.newTabSpec(getString(R.string.tabhost_mine))
                .setIndicator(mineIndicator), MineFragment.class, null);



    }


    /**
     * 底部导航布局
     *
     * @param resId
     * @param drawableId
     * @return
     */

    private View getIndicatorView(int resId, int drawableId) {

        View v = getLayoutInflater().inflate(R.layout.indicator_view, null);
        ImageView imageView = (ImageView) v.findViewById(R.id.main_indicator_img);
        imageView.setImageResource(drawableId);
        TextView textView = (TextView) v.findViewById(R.id.main_indicator_name);
        textView.setText(getResources().getString(resId));
        return v;
    }

    /**
     * 双击退出程序
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {

        if (!isExit) {
            isExit = true;
            ToastUtil.showShortToast(this, "再按一次退出程序");
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }


}
